package com.sgcc.fj.pmos.e_fj_order.presenter;

import android.text.TextUtils;

import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.fj.pmos.common.base.BasePresenter;
import com.sgcc.fj.pmos.common.bean.BaseResponse;
import com.sgcc.fj.pmos.common.http.HttpInterConst;
import com.sgcc.fj.pmos.common.http.HttpRequestUtils;
import com.sgcc.fj.pmos.common.http.MvpNetCallback;
import com.sgcc.fj.pmos.e_fj_order.bean.EnergyByMonthDetailBean;
import com.sgcc.fj.pmos.e_fj_order.contract.DaypartingEleChangesContract;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class DaypartingEleChangesPresenter extends BasePresenter<DaypartingEleChangesContract.View> implements DaypartingEleChangesContract.Presenter {

    @Override
    public void powerChange(String json) {
        HttpRequestUtils.getInstance().publicRequest(json, HttpInterConst.ORDER_POWERCHANGE, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                if (response != null) {
                    if ("200".equals(response.getCode())) {
                        getView().powerChange(response);
                    } else {
                        if (TextUtils.isEmpty(response.getMsg())) {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }
                }

            }
        }, null);

    }

    @Override
    public void getEnergyByMonthDetail(String relationId, String packageId) {
        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());
        params.put("relationId", relationId);
        params.put("packageId", packageId);
        Map<String, String> headers = new HashMap<>();
        headers.put("CurrentRoute", "/pxf-phbsx-shop/#/contractChange");
        HttpRequestUtils.getInstance().requestWithDiffHeaders(params, HttpInterConst.ORDER_GETENERGYBYMONTHDETAIL, new MvpNetCallback<EnergyByMonthDetailBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
            }

            @Override
            public void onResponse(EnergyByMonthDetailBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                if (response != null) {
                    if ("200".equals(response.getCode())) {
                        getView().getEnergyByMonthDetail(response);
                    } else {
                        if (TextUtils.isEmpty(response.getMsg())) {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }
                }

            }
        },headers);
    }
}
